import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;


public class TCP2Qdemo {
    public static void main(String[] args) {
        try {


            ServerSocket serverSocket = new ServerSocket(9002);
            System.out.println("服务器在9999端口监听，等待连接...");
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();
            while (true) {


                //4.IO读取
                byte[] buf = new byte[1024];
                int readLen = 0;
                while ((readLen = inputStream.read(buf)) != -1) {
                    System.out.println(new String(buf, 0, readLen));//根据读取到的实际长度，显示内容
                }
                //5.关闭流和socket
                inputStream.close();
            }

            // 释放资源

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
